package com.linesum.code.design.Observer;

import java.util.Iterator;
import java.util.List;

import com.google.common.collect.Lists;

/**
* 
* @author chenzf
* @date 2017年8月1日
*/
public class WeatherData implements Subject {
	private List<Observer> observers;
	private Double temp;
	private Double humidity;
	private Double pressure;

	public WeatherData() {
		this.observers = Lists.newArrayList();
	}

	@Override
	public void registerObserver(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		if (observers.contains(observer)) {
			observers.remove(observer);
		}
	}

	@Override
	public void notifyObservers() {
		Iterator<Observer> iterator = observers.iterator();
		while (iterator.hasNext()) {
			iterator.next().update(temp, humidity, pressure);
		}
	}

	/**
	 * 每次更新数据调用
	 * @param temp
	 * @param humidity
	 * @param pressure
	 * @author chenzf
	 * @date 2017年8月1日
	 */
	public void setMeasurements(Double temp, Double humidity, Double pressure) {
		this.temp = temp;
		this.humidity = humidity;
		this.pressure = pressure;
		notifyObservers();
	}

	@SuppressWarnings("unused")
	public static void main(String[] args) {
		WeatherData weatherData = new WeatherData();
		new CurrentConditionsDisplay(weatherData);
		weatherData.setMeasurements(10D, 10D, 10D);
		weatherData.setMeasurements(11D, 1D, 2D);

	}
}
